package org.liaohailong.helloworld;

import org.junit.Test;
import org.liaohailong.helloworld.practice.DPPractice;
import org.liaohailong.helloworld.practice.DesignPractice;
import org.liaohailong.helloworld.practice.MathPractice;
import org.liaohailong.helloworld.practice.OtherPractice;

import java.util.List;

/**
 * Author: liaohailong
 * Time: 2021/5/12 09:46
 * Describe:
 */
public class Practice2 implements AUtil.ILog {
    @Test
    public void maxProfit() {
        DPPractice dp = new DPPractice();
        // [7,1,5,3,6,4] = 5
        // [7,6,4,3,1] = 0
        // [1,2,3,4,5] = 4
        int[] prices = new int[]{7, 1, 5, 3, 6, 4};
//        int[] prices = new int[]{7,6,4,3,1};
//        int[] prices = new int[]{1, 2, 3, 4, 5};
        int profit = dp.maxProfit(prices);
        log("profit = " + profit);
    }

    @Test
    public void maxSubArray() {
        DPPractice dp = new DPPractice();
//        [-2,1,-3,4,-1,2,1,-5,4] = 6
        int[] arr = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
        int i = dp.maxSubArray(arr);
        log("i = " + i);
    }

    @Test
    public void rob() {
        DPPractice dp = new DPPractice();
//        int[] nums = new int[]{1, 2, 3, 1};
//        int[] nums = new int[]{2, 7, 9, 3, 1};
        int[] nums = new int[]{2, 1, 1, 2};
        int rob = dp.rob(nums);
        log("rob = " + rob);
    }

    @Test
    public void shuffle() {
        AUtil.setLog(this);
        int N = 1000000;
        int[] nums = new int[N];
        int start = -N / 2;
        for (int i = 0; i < N; i++) {
            nums[i] = start + i;
        }
        AUtil.print100(nums);
        DesignPractice.Solution solution = new DesignPractice.Solution(nums);
        AUtil.printCostInMs(() -> {
            int[] shuffle = solution.shuffle();
            AUtil.print100(shuffle);
        }, "shuffle");
    }

    @Test
    public void minStack() {
        DesignPractice.MinStack minStack = new DesignPractice.MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-1);
        int min = minStack.getMin();
        int top = minStack.top();
        minStack.pop();
        int min1 = minStack.getMin();
        log("OK");
    }

    @Test
    public void sum() {
        DesignPractice dp = new DesignPractice();
        int N = 10;
        int[] arr = new int[N];
        for (int i = 1; i <= N; i++) {
            arr[i - 1] = i;
        }
        int sum = dp.sum(arr);
        log("sum = " + sum);
    }

    @Test
    public void fizzBuzz() {
        MathPractice mp = new MathPractice();
        List<String> strings = mp.fizzBuzz(15);
        log(strings.toString());
    }

    @Test
    public void countPrimes() {
        MathPractice mp = new MathPractice();
        int i = mp.countPrimes(10);
        log("i = " + i);
    }

    @Test
    public void isPowerOfThree() {
        MathPractice mp = new MathPractice();
        boolean powerOfThree = mp.isPowerOfThree(6);
        log("powerOfThree = " + powerOfThree);
    }

    @Test
    public void romanToInt() {
        MathPractice mp = new MathPractice();
//        String romanInt = "III";// 3
//        String romanInt = "IV";// 4
//        String romanInt = "IX";// 9
//        String romanInt = "LVIII";// 58
        String romanInt = "MCMXCIV";// 1994
        int i = mp.romanToInt(romanInt);
        log("i = " + i);
    }

    @Test
    public void hammingWeight() {
        OtherPractice op = new OtherPractice();
        int n = 11;
        int i = op.hammingWeight(n);
        log("i = " + i);
    }

    @Test
    public void hammingDistance() {
        OtherPractice op = new OtherPractice();
//        int x = 1;
//        int y = 4;
        int x = 3;
        int y = 1;
        int i = op.hammingDistance(x, y);
        log("i = " + i);
    }

    @Test
    public void reverseBits() {
        OtherPractice op = new OtherPractice();
        int n = Integer.parseInt("00000010100101000001111010011100", 2);
        int i = op.reverseBits(n);
        log("n = " + Integer.toString(n, 2));
        log("i = " + Integer.toString(i, 2));
    }


    @Test
    public void generate() {
        OtherPractice op = new OtherPractice();
        List<List<Integer>> generate = op.generate(5);
        log(generate.toString());
    }

    @Test
    public void isValid() {
        OtherPractice op = new OtherPractice();
//        String str = "()";
//        String str = "()[]{}";
//        String str = "(]";
//        String str = "([)]";
        String str = "{[]}";
        boolean valid = op.isValid(str);
        log(str + " is " + valid);
    }

    @Test
    public void missingNumber() {
        OtherPractice op = new OtherPractice();
        int[] nums = new int[]{3, 0, 1};
        int i = op.missingNumber(nums);
        log("i = " + i);
    }

    public void log(String msg) {
        log("Exam1", msg);
    }

    @Override
    public void log(String tag, String msg) {
        System.out.println(tag + ": " + msg);
    }
}
